<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>C:\WorkingCopy\ColorTest\csvformat.pl.html</title>
<meta name="Generator" content="Vim/7.2">
<meta http-equiv="content-type" content="text/html; charset=Big5">
<style type="text/css">
<!--
.Number { color: #ff0000; font-weight: bold; }
.Constant { color: #ff0000; }
.Statement { font-weight: bold; }
.Special { color: #ff0000; font-weight: bold; }
pre { font-family: monospace; color: #000000; background-color: #ffffff; }
body { font-family: monospace; color: #000000; background-color: #ffffff; }
.PreProc { color: #0000ff; }
.Comment { color: #008080; }
-->
</style>
</head>
<body>
<pre>
<span class="PreProc">#!perl -w</span>

<span class="Comment"># Time-stamp: &lt;2002/04/06, 13:12:13 (EST), maverick, csvformat.pl&gt;</span>
<span class="Comment"># Two pass CSV file to table formatter</span>

$delim = $#ARGV &gt;= <span class="Number">1</span> ? $ARGV[<span class="Number">1</span>] : <span class="Constant">'</span><span class="Constant">,</span><span class="Constant">'</span>;
<span class="Statement">print</span> STDERR <span class="Constant">&quot;</span><span class="Constant">Split pattern: </span>$delim<span class="Special">\n</span><span class="Constant">&quot;</span>;

<span class="Comment"># first pass</span>
<span class="Statement">open</span> F, <span class="Constant">&quot;</span><span class="Constant">&lt;</span>$ARGV<span class="Constant">[0]</span><span class="Constant">&quot;</span> <span class="Statement">or</span> <span class="Statement">die</span>;
<span class="Statement">while</span>(&lt;F&gt;)
{
  <span class="Statement">chomp</span>;
  $i = <span class="Number">0</span>;
  <span class="Statement">map</span> { $max[$_-&gt;[<span class="Number">1</span>]] = $_-&gt;[<span class="Number">0</span>] <span class="Statement">if</span> $_-&gt;[<span class="Number">0</span>] &gt; ($max[$_-&gt;[<span class="Number">1</span>]] || <span class="Number">0</span>) }
    (<span class="Statement">map</span> {[<span class="Statement">length</span> $_, $i++]} <span class="Statement">split</span>($delim));
}
<span class="Statement">close</span> F;

<span class="Statement">print</span> STDERR <span class="Constant">'</span><span class="Constant">Field width:   </span><span class="Constant">'</span>, <span class="Statement">join</span>(<span class="Constant">'</span><span class="Constant">, </span><span class="Constant">'</span>, @max), <span class="Constant">&quot;</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
<span class="Statement">print</span> STDERR <span class="Statement">join</span>(<span class="Constant">'</span><span class="Constant"> </span><span class="Constant">'</span>, <span class="Statement">map</span> {<span class="Constant">'</span><span class="Constant">-</span><span class="Constant">'</span> x $_} @max);

<span class="Comment"># second pass</span>
<span class="Statement">open</span> F, <span class="Constant">&quot;</span><span class="Constant">&lt;</span>$ARGV<span class="Constant">[0]</span><span class="Constant">&quot;</span> <span class="Statement">or</span> <span class="Statement">die</span>;
<span class="Statement">while</span>(&lt;F&gt;)
  {
  <span class="Statement">chomp</span>;
  $i = <span class="Number">0</span>;
  <span class="Statement">map</span> { <span class="Statement">printf</span>(<span class="Constant">&quot;</span><span class="Constant">%-</span>$max<span class="Constant">[</span>$_<span class="Constant">-&gt;[1]]s </span><span class="Constant">&quot;</span>, $_-&gt;[<span class="Number">0</span>]) }
    (<span class="Statement">map</span> {[$_, $i++]} <span class="Statement">split</span>($delim));
  <span class="Statement">print</span> <span class="Constant">&quot;</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
}
<span class="Statement">close</span> F;
</pre>
</body>
</html>
